package dataconnect;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import data.ChineseToPy;
import data.HistoryData;
import data.LoadHistoryData;
import data.TimeStock;

public class InsertStockInfo {
	public static void insertStockInfo() throws SQLException{
		LoadHistoryData lhd = new LoadHistoryData();
		HistoryData hd = lhd.loadData();
		Set<String> keySet = hd.keySet();
		Connection con = ConnectionFactory.getConnection();
		Statement stmt = con.createStatement();
		ChineseToPy ctpy = new ChineseToPy();
		
		int i = 0;
		for (Iterator<String> iterator = keySet.iterator(); iterator.hasNext();) {
			String code = iterator.next();
			TimeStock ts = hd.get(code);
			String insertSql = "insert stockinfo(number,name,shortname) values('"+code+"','"+
			ts.getStockName()+"','"+ctpy.getStringFirstPY(ts.getStockName())+"')";
			stmt.addBatch(insertSql);
			i++;
			if(i>100){
				stmt.executeBatch();
				stmt.clearBatch();
				i=0;
			}
		}
		stmt.executeBatch();
		stmt.close();
	}
}
